/**
 *	@brief	空を飛ぶ BG描画
 *	@author	Miyuki Iwasawa
 *	@date	07.06.20
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/buflen.h"
#include "system/palanm.h"
#include "gflib/touchpanel.h"
#include "field/field_matrix.h"
#include "field/field_place_name.h"
#include "application/app_tool.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"

#include "../pgear_sys.h"
#include "../pgear_frm.h"
#include "../pgear_act.h"
#include "pgmap_sys.h"

#define __BGSKY_BG_H_GLOBAL
#include "pgsky_bg.h"
#include "pgmap_sub.h"

//===========================================================================
///プロトタイプ
//===========================================================================
static void pgsky_BGLInit(PGMAP_SYS_WORK* wk);
static void pgsky_BGLRelease(PGMAP_SYS_WORK* wk);
static void pgsky_2DResInit(PGMAP_SYS_WORK* wk);
static void pgsky_2DResRelease(PGMAP_SYS_WORK* wk);
static void pgsky_BmpWinAdd(PGMAP_SYS_WORK* wk);
static void pgsky_BmpWinDel(PGMAP_SYS_WORK* wk);
static void pgsky_MsgDataInit(PGMAP_SYS_WORK* wk);
static void pgsky_MsgDataRelease(PGMAP_SYS_WORK* wk);
static void pgsky_SelBoxListInit(PGMAP_SYS_WORK* wk);
static void pgsky_SelBoxListRelease(PGMAP_SYS_WORK* wk);
static void pgsky_ActResInit(PGMAP_SYS_WORK* wk);
static void pgsky_ActResRelease(PGMAP_SYS_WORK* wk);
static void pgsky_MakeActor(PGMAP_SYS_WORK* wk);
static void pgsky_DelActorAll(PGMAP_SYS_WORK* wk);
static void pgsky_MapIniDraw(PGMAP_SYS_WORK* wk);

//===========================================================================
///プログラムエリア
//===========================================================================
/**
 *	@brief	マップの初期化
 */
int PGSky_AppInit(PGMAP_SYS_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		sys_VBlankFuncChange(NULL,NULL);
		sys_HBlankIntrStop();	//HBlank割り込み禁止

		GF_Disp_GX_VisibleControlInit();
		GF_Disp_GXS_VisibleControlInit();
		GX_SetVisiblePlane(0);
		GXS_SetVisiblePlane(0);

		WIPE_SetBrightness(WIPE_DISP_MAIN,WIPE_FADE_BLACK);
		WIPE_SetBrightness(WIPE_DISP_SUB,WIPE_FADE_BLACK);
		WIPE_ResetWndMask(WIPE_DISP_MAIN);
		WIPE_ResetWndMask(WIPE_DISP_SUB);

		//タッチパネル初期化
		InitTPSystem();
		InitTPNoBuff(2);
		break;
	case 2:
		//2Dリソース初期化
		pgsky_BGLInit(wk);
		pgsky_2DResInit(wk);
		pgsky_BmpWinAdd(wk);
		pgsky_MsgDataInit(wk);
		break;
	case 3:
		//アクターリソース作成
		pgsky_ActResInit(wk);
		//アクター生成前にパレットをセット
		pgsky_MakeActor(wk);
		pgsky_SelBoxListInit(wk);
		break;
	case 4:
		//初期画面構築
		pgsky_MapIniDraw(wk);
		sys_VBlankFuncChange(PokeGearVBlank,wk->sys);
		//アプリVBlank関数登録
		wk->sys->app_vfunc = PGMapSub_VBlank;
		wk->sub_seq = 0;
		return TRUE;
	}
	++wk->sub_seq;
	return FALSE;
}

/**
 *	@brief	マップの開放
 */
int PGSky_AppEnd(PGMAP_SYS_WORK* wk)
{
	//アプリVBlank関数登録削除
	wk->sys->app_vfunc = NULL;

	pgsky_SelBoxListRelease(wk);
	pgsky_DelActorAll(wk);
	pgsky_ActResRelease(wk);
	pgsky_MsgDataRelease(wk);
	pgsky_BmpWinDel(wk);
	pgsky_2DResRelease(wk);
	pgsky_BGLRelease(wk);

	StopTP();
	sys_VBlankFuncChange(NULL,NULL);
	return TRUE;
}

/**
 *	@brief	空を飛ぶかどうか選択
 */
int PGSky_JumpSelect(PGMAP_SYS_WORK* wk)
{
	u32 ret;
	u8 key_mode = 0;

	ret = SelectBoxMain(wk->list_wk);
	if(ret != SBOX_SELECT_NULL){
		key_mode = wk->list_wk->key_mode;
		SelectBoxExit(wk->list_wk);

		if(key_mode != wk->sys->key_mode){
#if 0
			if(key_mode == APP_END_KEY){
				PGMapSub_ToKeyChangeMap((void*)wk);
			}else{
				PGMapSub_ToTouchChangeMap((void*)wk);
			}
#endif
			wk->sys->key_mode = key_mode;
		}
		if(	ret == 0){
			wk->sys->param->retval = TRUE;
			return PGMAP_SEQ_ALL_FADEOUT;
		}
		PGMapSub_PrintExpMsg(wk,-1);
		return PGMAP_SEQ_MAIN;
	}
	return PGMAP_SEQ_JUMP_SELECT;
}


/**
 *	@brief	マップのBGL初期化
 */
static void pgsky_BGLInit(PGMAP_SYS_WORK* wk)
{
	int i;

	//VramBankセット
	PGFrame_VBankSet();

	//BGL初期化
	wk->sys->bgl = GF_BGL_BglIniAlloc(wk->heapID);

	//上画面をサブ,下画面をメインに切り替え
	GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

	//初期BGモードセット
	{
		GF_BGL_SYS_HEADER BGsys_data = {
			GX_DISPMODE_GRAPHICS,GX_BGMODE_5,GX_BGMODE_0,GX_BG0_AS_2D
		};
		GF_BGL_InitBG(&BGsys_data);
	}

	{
		GF_BGL_BGCNT_HEADER TextBgCntDat[] = {
		{	//MAIN BG0
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800,GX_BG_CHARBASE_0x08000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE},
		{	//MAIN BG1
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			1,0,0,FALSE},
		{	//MAIN BG2
			0,0,0x2000,0,GF_BGL_SCRSIZ_512x512,GX_BG_COLORMODE_256,
			GX_BG_SCRBASE_0xd000,GX_BG_CHARBASE_0x10000,GX_BG_EXTPLTT_01,
			2,0,0,FALSE},
		{	//MAIN BG3
			0,0,0x2000,0,GF_BGL_SCRSIZ_512x512,GX_BG_COLORMODE_256,
			GX_BG_SCRBASE_0xb000,GX_BG_CHARBASE_0x10000,GX_BG_EXTPLTT_01,
			3,0,0,FALSE},
		{	//SUB BG0
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800,GX_BG_CHARBASE_0x08000,GX_BG_EXTPLTT_01,
			3,0,0,FALSE},
		{	//SUB BG1
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE},
		{	//SUB BG2
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			1,0,0,FALSE},
		{	//SUB BG3
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe000,GX_BG_CHARBASE_0x10000,GX_BG_EXTPLTT_01,
			2,0,0,FALSE},
		};
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME0_M,&(TextBgCntDat[0]),GF_BGL_MODE_TEXT);
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME1_M,&(TextBgCntDat[1]),GF_BGL_MODE_TEXT);
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME2_M,&(TextBgCntDat[2]),GF_BGL_MODE_256X16);
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME3_M,&(TextBgCntDat[3]),GF_BGL_MODE_256X16);
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME0_S,&(TextBgCntDat[4]),GF_BGL_MODE_TEXT);
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME1_S,&(TextBgCntDat[5]),GF_BGL_MODE_TEXT);
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME2_S,&(TextBgCntDat[6]),GF_BGL_MODE_TEXT);
		GF_BGL_BGControlSet(wk->sys->bgl,GF_BGL_FRAME3_S,&(TextBgCntDat[7]),GF_BGL_MODE_TEXT);

		GF_Disp_GX_VisibleControl( GX_PLANEMASK_BG0|GX_PLANEMASK_BG1|GX_PLANEMASK_BG2|GX_PLANEMASK_BG3,VISIBLE_OFF);
		GF_Disp_GXS_VisibleControl(GX_PLANEMASK_BG0|GX_PLANEMASK_BG1|GX_PLANEMASK_BG2|GX_PLANEMASK_BG3,VISIBLE_OFF);

		for(i = 0;i < 8;i++){
			GF_BGL_ScrClear(wk->sys->bgl,GF_BGL_FRAME0_M+i);
			GF_BGL_ClearCharSet(GF_BGL_FRAME0_M+i,32,0,wk->heapID);
			GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME0_M+i);
		}
	}

}

/**
 *	@brief	マップBGL開放
 *
 */
static void pgsky_BGLRelease(PGMAP_SYS_WORK* wk)
{
	//ローカルパレット領域をクリア
	GF_BGL_ClearPaletteSet(GF_BGL_FRAME1_M,0x20*PGAPP_PALNUM_M,0 ,wk->heapID);
	GF_BGL_ClearPaletteSet(GF_BGL_FRAME1_S,0x20*PGAPP_PALNUM_S,0 ,wk->heapID);

	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME3_S);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME2_S);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME1_S);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME0_S);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME3_M);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME2_M);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME1_M);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME0_M);

	//bgl開放
	sys_FreeMemoryEz(wk->sys->bgl);

	//上画面をメイン,下画面をサブに戻す
	GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

	//ブレンド設定解除
	G2_SetBlendAlpha(GX_BLEND_PLANEMASK_NONE,GX_BLEND_PLANEMASK_NONE,0,0);
}

/**
 *	@brief	マップのBGリソース初期化
 */
static void pgsky_2DResInit(PGMAP_SYS_WORK* wk)
{
	int	arcID;
	ARCHANDLE* handle;

	//アーカイブハンドル取得
	arcID = ARC_PGMAP_GRA;
	handle = ArchiveDataHandleOpen(ARC_PGMAP_GRA,wk->heapID);

	//キャラクタ転送
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_skyfrm_ncgr,
		GF_BGL_FRAME1_M,0,0,0);

	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_map_ncgr,
		GF_BGL_FRAME2_M,0,0,0);

	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_skyback_ncgr,
		GF_BGL_FRAME0_S,0,0,0);
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_skyguide_ncgr,
		GF_BGL_FRAME2_S,0,0,0);
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_gmap_ncgr,
		GF_BGL_FRAME3_S,0,0,0);

	//パレット転送
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_skybgd_nclr,
		GF_BGL_FRAME1_M,2,0,0);

	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_skybgu_nclr,
		GF_BGL_FRAME0_S,2,0,0);

	//スクリーン転送
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgmap_gra_skyback_nscr,
		GF_BGL_FRAME0_S,1,0,0);

	wk->pSBufGuide = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_pgmap_gra_skyguide_nscr,FALSE,&wk->pSGuide,wk->heapID);
	wk->pSBufMap = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_pgmap_gra_map_nscr,FALSE,&wk->pSMap,wk->heapID);
	wk->pSBufGMap = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_pgmap_gra_gmap_nscr,FALSE,&wk->pSGMap,wk->heapID);
	wk->pSBufFrm = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_pgmap_gra_skyfrm_nscr,FALSE,&wk->pSFrm,wk->heapID);

	//ハンドル閉じる
	ArchiveDataHandleClose( handle );

	//スクリーン転送
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME0_S);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME1_M);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME2_M);

	//ここでタッチフォントも追加しておく
	FontProc_LoadFont( FONT_TOUCH, wk->heapID );
}

/**
 *	@brief	マップのBGリソース開放
 */
static void pgsky_2DResRelease(PGMAP_SYS_WORK* wk)
{
	FontProc_UnloadFont( FONT_TOUCH );

	sys_FreeMemoryEz(wk->pSBufFrm);
	sys_FreeMemoryEz(wk->pSBufGMap);
	sys_FreeMemoryEz(wk->pSBufMap);
	sys_FreeMemoryEz(wk->pSBufGuide);
}

/**
 *	@brief	BmpWin初期化
 */
static void pgsky_BmpWinAdd(PGMAP_SYS_WORK* wk)
{
	int i;

	static const struct{
		u8 frm,px,py,sx,sy,pal;
		u16	cgx;
	}windat[] = {
	 { BMPL_G01_FRM,BMPL_G01_PX,BMPL_G01_PY,BMPL_G01_SX,BMPL_G01_SY,
	   BMPL_G01_PAL,BMPL_G01_CGX},
	 { BMPL_G02_FRM,BMPL_G02_PX,BMPL_G02_PY-1,BMPL_G02_SX,BMPL_G02_SY,
	   BMPL_G02_PAL,BMPL_G02_CGX},
	 { BMPL_G03_FRM,BMPL_G03_PX,BMPL_G03_PY,BMPL_G03_SX,BMPL_G03_SY,
	   BMPL_G03_PAL,BMPL_G03_CGX},
	 { BMPL_MK01_FRM,BMPL_MK01_PX,BMPL_MK01_PY,BMPL_MK01_SX,BMPL_MK01_SY,
	   BMPL_MK01_PAL,BMPL_MK01_CGX},
	 { BMPL_MK02_FRM,BMPL_MK02_PX,BMPL_MK02_PY,BMPL_MK02_SX,BMPL_MK02_SY,
	   BMPL_MK02_PAL,BMPL_MK02_CGX},
	 { BMPL_MK03_FRM,BMPL_MK03_PX,BMPL_MK03_PY,BMPL_MK03_SX,BMPL_MK03_SY,
	   BMPL_MK03_PAL,BMPL_MK03_CGX},
	 { BMPL_MK04_FRM,BMPL_MK04_PX,BMPL_MK04_PY,BMPL_MK04_SX,BMPL_MK04_SY,
	   BMPL_MK04_PAL,BMPL_MK04_CGX},
	 { BMPL_EXP01_FRM,BMPL_EXP01_PX,BMPL_EXP01_PY,BMPL_EXP01_SX,BMPL_EXP01_SY,
	   BMPL_EXP01_PAL,BMPL_EXP01_CGX},
	 { BMPL_EXP02_FRM,BMPL_EXP02_PX,BMPL_EXP02_PY,BMPL_EXP02_SX,BMPL_EXP02_SY,
	   BMPL_EXP02_PAL,BMPL_EXP02_CGX},
	};
	for(i = 0;i < WIN_MAX;i++){
		GF_BGL_BmpWinAdd(
			wk->sys->bgl, &wk->win[i], windat[i].frm,
			windat[i].px, windat[i].py, windat[i].sx, windat[i].sy, windat[i].pal, windat[i].cgx );
		GF_BGL_BmpWinDataFill(&(wk->win[i]),FBMP_COL_NULL);
	}
}

/*
 *	@brief	BmpWin開放
 */
static void pgsky_BmpWinDel(PGMAP_SYS_WORK* wk)
{
	int i = 0;

	for(i = 0;i < WIN_MAX;i++){
		GF_BGL_BmpWinOff(&wk->win[i]);
		GF_BGL_BmpWinDel(&wk->win[i]);
	}
}

/**
 *	@brief	メッセージデータ初期化
 */
static void pgsky_MsgDataInit(PGMAP_SYS_WORK* wk)
{
	wk->msgDat.gMan = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,
			NARC_msg_pgmap_guide_dat,wk->heapID);

	wk->msgDat.wset = WORDSET_CreateEx(2,WORDSET_PGMAP_BUFLEN,wk->heapID);
	wk->msgDat.tmp = STRBUF_Create(WORDSET_PGMAP_BUFLEN,wk->heapID);
	wk->msgDat.area[0] = MSGMAN_AllocString(wk->msgDat.gMan,msg_area_west);
	wk->msgDat.area[1] = MSGMAN_AllocString(wk->msgDat.gMan,msg_area_east);
	wk->msgDat.place = STRBUF_Create((MAPNAME_MAX+BUFLEN_EOM_SIZE)*2,wk->heapID);
	wk->msgDat.exp[0] = MSGMAN_AllocString(wk->msgDat.gMan,msg_sky_exp01);
	wk->msgDat.exp[1] = MSGMAN_AllocString(wk->msgDat.gMan,msg_sky_exp02);
	wk->msgDat.exp[2] = MSGMAN_AllocString(wk->msgDat.gMan,msg_sky_exp03);
	wk->msgDat.pms_word = MSGMAN_AllocString(wk->msgDat.gMan,msg_mk_word);

	//メッセージスピード取得
//	wk->msgDat.msg_spd = CONFIG_GetMsgPrintSpeed(wk->param->configSave);
	wk->msgDat.msg_spd = 2;
}

/*
 *	@brief	メッセージデータ開放
 */
static void pgsky_MsgDataRelease(PGMAP_SYS_WORK* wk)
{
	STRBUF_Delete(wk->msgDat.pms_word);
	STRBUF_Delete(wk->msgDat.exp[2]);
	STRBUF_Delete(wk->msgDat.exp[1]);
	STRBUF_Delete(wk->msgDat.exp[0]);
	STRBUF_Delete(wk->msgDat.place);
	STRBUF_Delete(wk->msgDat.area[1]);
	STRBUF_Delete(wk->msgDat.area[0]);
	STRBUF_Delete(wk->msgDat.tmp);
	WORDSET_Delete(wk->msgDat.wset);

	MSGMAN_Delete(wk->msgDat.gMan);
}
/**
 *	@brief	選択ボックスリストの初期化
 */
static void pgsky_SelBoxListInit(PGMAP_SYS_WORK* wk)
{
	int i;

	//BMPリスト領域作成
	wk->plist = BMP_MENULIST_Create(SBOX_COUNT,wk->heapID);
	for(i = 0;i < 2;i++){
		BMP_MENULIST_AddArchiveString(wk->plist,wk->msgDat.gMan,msg_sky_sel01+i,i);
	}
}

/**
 *	@brief	選択ボックスリストの解放
 */
static void pgsky_SelBoxListRelease(PGMAP_SYS_WORK* wk)
{
	//リスト解放
	BMP_MENULIST_Delete(wk->plist);

	wk->plist = NULL;
}

/**
 *	@brief	マップのアクターリソース初期化
 */
static void pgsky_ActResInit(PGMAP_SYS_WORK* wk)
{
	PGear_ActorSysInit(wk->sys);

	//リソース確保
	PGear_ActorResInit(wk->sys,PGAPP_MODE_MAP);
	//ワークマネージャ作成
	wk->pActWMan = PGActWorkMan_Create(PGSKY_ACTMAX,wk->heapID);

	//サブサーフェースの距離を設定
	CLACT_U_SetSubSurfaceMatrix(CATS_EasyRenderGet(wk->sys->pActSys),0,
			SUB_SURFACE_Y+FX32_CONST(PGMAP_SUBSURFACE_Y));

	wk->sbox = SelectBoxSys_AllocWork(wk->heapID ,wk->sys->palAnm );
}

/**
 *	@brief	マップのアクターリソース開放
 */
static void pgsky_ActResRelease(PGMAP_SYS_WORK* wk)
{
	//選択ボックスシステムワーク解放
	SelectBoxSys_Free(wk->sbox);

	//サブサーフェースの距離を設定
	CLACT_U_SetSubSurfaceMatrix(CATS_EasyRenderGet(wk->sys->pActSys),0,SUB_SURFACE_Y);

	//ワークマネージャ開放
	PGActWorkMan_Release(wk->pActWMan);
	//リソース開放
	PGear_ActorResRelease(wk->sys);
	PGear_ActorSysRelease(wk->sys);
}

/**
 *	@biref	マップの　コモンアクター作成
 */
static void pgsky_MakeActor(PGMAP_SYS_WORK* wk)
{
	int i;
	u16	idx;
	PGEAR_ACT_WORK*  pAct= wk->pActWMan->pAct;

	static const TCATS_OBJECT_ADD_PARAM	ActAddParam[] =
	{
		{	// 上画面ガイドマーク
			RESACT_MARK,	//ID
			NACT_HERO_PX, NACT_HERO_PY, 0,	//x,y,z
			ANM_MK_ICON, 0, PGSYSOAM_PALIDX_M, NNS_G2D_VRAM_TYPE_2DSUB,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 上画面ガイドマーク 再戦トレーナーアイコン
			RESACT_MARK,	//ID
			NACT_REVENGE_PX, NACT_REVENGE_PYS+PGMAP_SUBSURFACE_Y, 0,	//x,y,z
			ANM_MK_REVENGE, 0, PGSYSOAM_PALIDX_M, NNS_G2D_VRAM_TYPE_2DSUB,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_HERO,	//ID
			NACT_CUR_PX, NACT_CUR_PY, 0,	//x,y,z
			ANM_H_CURSOR, 1, PGSYSOAM_PALIDX_M+0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 主人公
			RESACT_HERO,	//ID
			NACT_HERO_PX, NACT_HERO_PY, 0,	//x,y,z
			ANM_H_HERO, 2, PGSYSOAM_PALIDX_M, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// ポケモン
			RESACT_HERO,	//ID
			NACT_HERO_PX, NACT_HERO_PY, 0,	//x,y,z
			ANM_H_POKE, 2, PGSYSOAM_PALIDX_M, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_HERO,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_MKCUR_LT, 1, PGSYSOAM_PALIDX_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_HERO,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_MKCUR_LB, 1, PGSYSOAM_PALIDX_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_HERO,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_MKCUR_RT, 1, PGSYSOAM_PALIDX_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_HERO,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_MKCUR_RB, 1, PGSYSOAM_PALIDX_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 空を飛ぶマーカー
			RESACT_HERO,	//ID
			0,0, 0,	//x,y,z
			ANM_H_SKY01, 3, PGSYSOAM_PALIDX_M+0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
	};
	//上画面ガイドアイコン
	for(i = 0;i < 4;i++){
		PGActWorkMan_ListAdd(wk->pActWMan,
					CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[0]));
		CATS_ObjectPosSet(pAct[NACT_MARK1+i].act,
				NACT_MARK_PX+NACT_MARK_SX*(i%2),
				NACT_MARK_PY+(i/2)*NACT_MARK_SY+PGMAP_SUBSURFACE_Y);
		CATS_ObjectBGPriSet(pAct[NACT_MARK1+i].act,0);
	}
	//上画面再戦トレーナーアイコン
	PGActWorkMan_ListAdd(wk->pActWMan,
		CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[1]));
	CATS_ObjectBGPriSet(pAct[NACT_REV].act,0);

	//カーソル
	PGActWorkMan_ListAdd(wk->pActWMan,
			CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[2]));
	//主人公
	PGActWorkMan_ListAdd(wk->pActWMan,
			CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[3]));

	//ポケモン
	for(i = 0;i < MOVE_POKE_MAX;i++){
		PGActWorkMan_ListAdd(wk->pActWMan,
				CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[4]));
		CLACT_AnmFrameChg(pAct[NACT_POKE1+i].act, FX32_CONST(i));
	}

	//選択カーソル
	for(i = 0;i < 4;i++){
		idx = PGActWorkMan_ListAdd(wk->pActWMan,
					CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[5+i]));
		CATS_ObjectPosGet(pAct[idx].act,&pAct[idx].pos.x,&pAct[idx].pos.y);
		CATS_ObjectBGPriSet(pAct[idx].act,0);
		CLACT_SetAnmFlag(pAct[idx].act, TRUE);
		CLACT_SetDrawFlag(pAct[idx].act, FALSE);
	}

	for(i = 0;i < SACT_NUM;i++){
		//ワークリストに登録
		idx = PGActWorkMan_ListAdd(wk->pActWMan,
					CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[9]));

		CATS_ObjectPosGet(pAct[idx].act,&pAct[idx].pos.x,&pAct[idx].pos.y);
		CLACT_SetDrawFlag(pAct[idx].act, FALSE);
		CLACT_SetAnmFlag(pAct[idx].act, FALSE);
	}

	//初期位置セット
	for(i = 0;i < NACT_NUM;i++){
		idx = NACT_START+i;
		CATS_ObjectPosGet(pAct[idx].act,&pAct[idx].pos.x,&pAct[idx].pos.y);
		CLACT_SetDrawFlag(pAct[idx].act, FALSE);
	}
	CLACT_SetAnmFlag(pAct[NACT_CURSOR].act, TRUE);
	CLACT_SetAnmFlag(pAct[NACT_HERO].act, FALSE);
	CLACT_AnmFrameSet(pAct[NACT_HERO].act,wk->sys->param->player_sex);

	//カーソルはアフィンフラグを立てておく
	CLACT_SetAffineParam(pAct[NACT_CURSOR].act,CLACT_AFFINE_DOUBLE);
}

/**
 *	@brief	マップ　アクター全削除
 */
static void pgsky_DelActorAll(PGMAP_SYS_WORK* wk)
{
	PGActWorkMan_ListDelAll(wk->pActWMan);
}

/*
 *	@brief	初期画面描画
 */
static void pgsky_MapIniDraw(PGMAP_SYS_WORK* wk)
{
	int i;
	s16	cx,cy,ofs;
	PGEAR_ACT_WORK* pAct = wk->pActWMan->pAct;

	//ブレンド設定
	G2_SetBlendAlpha(GX_BLEND_PLANEMASK_BG2,GX_BLEND_PLANEMASK_BG3,MAP_ALPHA_SECOND,MAP_ALPHA_FIRST);

	//いったんスクリーンをクリア
	for(i = 0;i < 3;i++){
		GF_BGL_ScrClear(wk->sys->bgl,GF_BGL_FRAME1_M+i);
		GF_BGL_ScrollSet( wk->sys->bgl,GF_BGL_FRAME1_M+i, GF_BGL_SCROLL_X_SET, 0);
		GF_BGL_ScrollSet( wk->sys->bgl,GF_BGL_FRAME1_M+i, GF_BGL_SCROLL_Y_SET, 0);
	}

	//キーとタッチ切替時に呼ぶ関数を設定
	wk->sys->to_akey_func = PGMapSub_ToKeyChangeMap;
	wk->sys->to_touch_func = PGMapSub_ToTouchChangeMap;

	//初期スクロール位置取得
	PGMapSub_MapBGIniScrollCalc(wk);
	PGMapSub_MapBGScrollSet(wk,&wk->cScrl);

	//フレーム書き込み
	GF_BGL_ScrWriteExpand(wk->sys->bgl,GF_BGL_FRAME1_M,
		0,0,32,24,
		wk->pSFrm->rawData,
		0,0,
		wk->pSFrm->screenWidth/8,wk->pSFrm->screenHeight/8);

	//タウンマップBGの初期表示
	PGMapSub_DrawTownMap(wk);

	wk->jump_target = -1;
	if(wk->map_mode == MAP_MODE_BG){
		//ガイド初期表示
		GF_BGL_ScrWriteExpand(wk->sys->bgl,GF_BGL_FRAME2_S,
			0,8,32,6,wk->pSGuide->rawData,0,8,
			wk->pSGuide->screenWidth/8,wk->pSGuide->screenHeight/8);

		PGMapSub_GetNowZone(wk,&(wk->cZone),wk->cTarget.x,wk->cTarget.z);
		PGMapSub_DrawGuide(wk,TRUE);
		GF_BGL_ScrollSet(wk->sys->bgl,GF_BGL_FRAME1_S,GF_BGL_SCROLL_Y_SET,-(10*8+1));
		GF_BGL_ScrollSet(wk->sys->bgl,GF_BGL_FRAME2_S,GF_BGL_SCROLL_Y_SET,-(10*8+1));
		GF_BGL_ScrollSet(wk->sys->bgl,GF_BGL_FRAME3_S,GF_BGL_SCROLL_Y_SET,-(10*8+1));
	}else{
		//空を飛ぶアイコンの初期描画
		PGMapSub_SkyPosViewSet(wk,0);

		//ガイド初期表示
		GF_BGL_ScrWriteExpand(wk->sys->bgl,GF_BGL_FRAME2_S,
			0,8,32,16,wk->pSGuide->rawData,0,8,
			wk->pSGuide->screenWidth/8,wk->pSGuide->screenHeight/8);

#ifdef PM_DEBUG
		if(wk->debug_f){
			PGMapSub_GetNowZone(wk,&(wk->cZone),wk->cTarget.x,wk->cTarget.z);
		}else{
			PGMapSub_GetNowZoneSky(wk,&(wk->cZone),wk->cTarget.x,wk->cTarget.z);
		}
#else
		PGMapSub_GetNowZoneSky(wk,&(wk->cZone),wk->cTarget.x,wk->cTarget.z);
#endif
		PGMapSub_DrawGuide(wk,FALSE);

		PGMapSub_PrintExpMsg(wk,-1);
	}
	PGMapSub_DrawTargetZoneHilight(wk,TRUE);

	//MapOBJの状態変更
	PGMapSub_MapObjSetIOMark(wk,1);

	//操作説明メッセージ表示

	//やめるボタン表示
	ofs = FontProc_GetPrintStrWidth(FONT_TOUCH,wk->msgDat.exp[2],0);
	ofs = ((BMPL_EXP02_SX*8)-ofs)/2;
	GF_STR_PrintColor( &(wk->win[WIN_EXP02]),FONT_TOUCH,
						wk->msgDat.exp[2],
						ofs,0,MSG_ALLPUT,FCOL_SKY_BACK,NULL );


	//キー操作モードによって、描画状態を変更
	CLACT_SetDrawFlag(pAct[NACT_CURSOR].act,TRUE);

	PGActWorkMan_AllPosUpdate(wk->pActWMan);

	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME0_S);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME1_S);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME3_S);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME0_M);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME1_M);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME2_M);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME3_M);
}